package indi.kider.server.applet.api;

import indi.kider.common.model.apabus.LeaseBus;
import indi.kider.common.model.apabus.LeaseBusVo;
import indi.kider.common.model.apabus.LeaseModel;
import indi.kider.server.applet.service.LeaseBusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pers.vankid.common.model.base.CommonResponse;

/**
 * 租用业务
 *
 * @author wangjingjing
 * @create 2018-10-20 下午3:53
 **/
@RestController
@RequestMapping(value = "leaseBusiness", produces = "application/json;charset=utf8")
public class LeaseBusinessController {

    @Autowired
    private LeaseBusService leaseBusService;

    /**
     * 退房申请
     *
     * @param leaseModel
     * @return
     */
    @PostMapping("/retreat")
    public CommonResponse retreat(@RequestBody LeaseModel leaseModel) {
        try {
            leaseBusService.applyfor(leaseModel, 1);
            return CommonResponse.buildAddSuccessResponse("true");
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 换房申请
     *
     * @param leaseModel
     * @return
     */
    @PostMapping("/changeRoom")
    public CommonResponse changeRoom(@RequestBody LeaseModel leaseModel) {
        try {
            leaseBusService.applyfor(leaseModel, 2);
            return CommonResponse.buildAddSuccessResponse("true");
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 查询用户的退房和换房的状态
     *
     * @param rentId
     * @return
     */
    @PostMapping("/queryLeaseModel/{rentId}")
    public CommonResponse<LeaseBusVo> queryLeaseModel(Long rentId) {
        try {
            LeaseBus leaseBus = leaseBusService.queryStateByTenantId(rentId);
            return CommonResponse.buildGetSuccessResponse(leaseBusService.trucLeaseBus(leaseBus));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }
}
